//
//  RegisterCtr.swift
//  travel
//
//  Created by tong on 16/7/15.
//  Copyright © 2016年 zhuxietong. All rights reserved.
//

import UIKit
import Eelay

extension EeRegisterCtr
{
    
    @objc open func registerAction() {
        currentT.resignFirstResponder()
        
        let info = JoPaw.paw(fields: self.usernameT,self.codeT,self.passwordT)
        if info.validate
        {
            weak var wself = self
            CoHud.loading(wself?.navigationController?.view)
            co_delay(delay: 1.0, closure: {
                CoHud.dismiss(wself?.navigationController?.view)
                _ = wself?.navigationController?.popViewController(animated: true)
                
                let username = "\(wself!.usernameT.text!)"
                let password = "\(wself!.passwordT.text!)"
                co_delay(delay: 0.34, closure: {
                    EeRegisterCtr.success_register(["username":username,"password":password])
                })
                
            })
        }
        
    }
    
    @objc func getCodeAction() {
        checkCode()
    }
}

open class EeRegisterCtr: CoInputController
{
    
    public var usernameT = JoTextField(id: "phone", defaultV: nil, need: true, hiden: false, rule: JoRule(reg: "^\\d{6,}$", nil_msg: "请输入手机号".loc, err_msg: "检查手机号".loc))
    
    public var codeT = JoTextField(id: "code", defaultV: nil, need: true, hiden: false, rule: JoRule(reg: EeLogin.code, nil_msg: "请输入验证码".loc, err_msg: "检查验证码".loc))
    
    public var passwordT = JoTextField(id: "password", defaultV: nil, need: true, hiden: false, rule: JoRule(reg: EeLogin.password, nil_msg: "请输入密码".loc, err_msg: "检查密码".loc))
    
    
    public var codeBt = UIButton()
    
    public var waitingSecond = 60
    public var inWaiting = false
    
    public var timer:Timer?
    
    public static var success_register:([String:Any]) ->Void = {_ in}
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        layoutView()
    }
    open func layoutView(){
        self.title = ""
        self.top_style = .transparent_dark
        
        
        inputBack.eelay = [
            [cornerV,280,[ee.X.Y,[0,-100]]],
        ]
        
        usernameT.delegate = self
        passwordT.delegate = self
        
        usernameT.placeholder = "电话号码".loc
        passwordT.placeholder = "请输入密码".loc
        passwordT.isSecureTextEntry = true
        
        inputBack.backgroundColor = .white
        
        _ = usernameT.ui.font16
        _ = passwordT.ui.font16
        
        let line1 = UIView()
        line1.bsui.background(hex:"#666" )
        let line2 = UIView()
        line2.bsui.background(hex:"#666" )
        
        let logoL = UILabel()
        
        let bkview = UIView()
        cornerV.eelay = [
            [logoL,[ee.T.X]],
            [bkview,[logoL,ee.B,ee.T,20],[ee.B.L.R]],
            [usernameT,[ee.L.R,[16,-16]],"50",[logoL,ee.B,ee.T,20]],
            [codeT,[ee.L.R,[16,-140]],"50",[usernameT,ee.B,ee.T,0]],
            [passwordT,[ee.B.L.R,[0,16,-16]],"50",[codeT,ee.B,ee.T,0]],
            [codeBt,[codeT,ee.Y],[ee.R,-10],"30",90],
            [line1,[ee.L.R,[0,0]],[usernameT,ee.B],"0.5"],
            [line2,[ee.L.R,[0,0]],[codeT,ee.B],"0.5"],
            
        ]
        
        bkview.bsui.corner5.background(hex: "#fff")
        bkview.bsui.set(radius: 1, width: 1, color: UIColor(shex:"#aaa"))
        _ = logoL.ui.font48.cl_666.text("IWE")
        
        usernameT.ui.font15.text(hex: "#666")
        codeT.ui.font15.text(hex: "#666")
        passwordT.ui.font15.text(hex: "#666")
        usernameT.tintColor = UIColor(shex: EeLogin.__main_hex)
        codeT.tintColor = UIColor(shex: EeLogin.__main_hex)
        passwordT.tintColor = UIColor(shex: EeLogin.__main_hex)
        
        usernameT.setPlaceholder(info: ("请输入手机号".loc,UIColor(shex:"#aaa"),UIFont.systemFont(ofSize: 14)))
        codeT.setPlaceholder(info: ("请输入验证码".loc,UIColor(shex:"#aaa"),UIFont.systemFont(ofSize: 14)))
        passwordT.setPlaceholder(info: ("请输入密码".loc,UIColor(shex:"#aaa"),UIFont.systemFont(ofSize: 14)))
        
        let loginBt = UIButton()
        let registerBt = UIButton()
        
        
        inputBack.eelay = [
            //            [pwdBt,[cornerV,ee.R],[cornerV,ee.B,ee.T,8]],
            [registerBt,"40",200,[ee.X],[cornerV,ee.B,ee.T,40]],
        ]
        
        
        cornerV.backgroundColor = .clear
        
        _ = loginBt.ui.buttonColor(hex:EeLogin.__main_hex).ncolor(hex: "#fff").ntitle(title: "登录").font(size: 17).corner3
        
        //        _ = loginBt.ui.ncolor(hex: "#fff").ntitle(title: "登录").font(size: 17).set(radius: 3, width: 1, color: .white)
        _ = registerBt.ui.ncolor(hex: "#aaa").ntitle(title: "注册新用户".loc).font(size: 14)
        
        
        
        
        
        _ = codeBt.ui.buttonColor(hex: EeLogin.__main_hex).ncolor(hex: "#fff").ntitle(title: "获取验证码".loc).font(size: 14).corner4
        
        codeBt.addTarget(self, action: #selector(getCodeAction), for: .touchUpInside)
        
        
        _ = registerBt.ui.buttonColor(hex: EeLogin.__main_hex).ncolor(hex: "#fff").ntitle(title: "注册".loc).font(size: 17).corner3
        
        registerBt.addTarget(self, action: #selector(registerAction), for: .touchUpInside)
        
        self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(waitCode), userInfo: nil, repeats: true)
        self.timer?.fire()
    }
    
    open override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    @objc open func checkCode()
    {
        
        if !inWaiting
        {
            self.currentT.resignFirstResponder()
            
            let result = JoPaw.paw(fields: self.usernameT)
            
            if result.validate
            {
                
                weak var wself = self
                CoHud.loading(wself?.navigationController?.view)
                co_delay(delay: 1.0, closure: {
                    CoHud.dismiss(wself?.navigationController?.view)
                    
                    self.waitingSecond = 60
                    self.inWaiting = true
                    "验证码已发送(123456)".alert()
                    return
                })
            }
        }
        
    }
    
    @objc open func waitCode()
    {
        if  inWaiting
        {
            waitingSecond = waitingSecond-1
            codeBt.setTitle("等待[XX]秒".replaceVars(values: "\(waitingSecond)"), for: .normal)
        }
        if waitingSecond <= 0
        {
            inWaiting = false
            self.waitingSecond = 60
            codeBt.setTitle("获取验证码".loc, for: .normal)
        }
    }
    
    
    
    
    
}

